package com.dgut.smart_house.mapper;

import com.dgut.smart_house.domain.DatasForKeyCard;
import com.dgut.smart_house.domain.KeyCard;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
@Repository
public interface KeyCardMapper {

    @Select("select * from key_card where key_id=#{keyId}")
    public KeyCard findCardByKey(int keyId);

    @Select("select * from key_card where card_id=#{cardId}")
    public KeyCard findCardByCardId(long cardId);

    @Select("select * from key_card where from_user=#{fromUser} and card_id != ''")
    public List<KeyCard> findAllCard(int fromUser);

    @Insert("insert into key_card (card_desc,from_user)values(#{cardDesc},#{fromUser})")
    public Integer createCardMaster(KeyCard keyCard);

    @Delete("delete from key_card where key_id=#{keyId}")
    public Integer deleteCardMaster(int keyId);

    @Update("update key_card set card_desc=#{cardDesc} where key_id=#{keyId}")
    public Integer updateCardMaster(@Param("cardDesc") String cardDesc, @Param("keyId") int keyId);

    @Insert("insert into datas_for_key_card (key_id,card_desc,from_user) values (#{keyId},#{cardDesc},#{fromUser})")
    public int createData(KeyCard keyCard);

    @Select("select * from datas_for_key_card where from_user=#{fromUser}")
    public List<DatasForKeyCard> findAllCardLog(int fromUser);

    @Select("select * from key_card where from_user=#{fromUser} and card_id is NULL order by key_id desc limit 1")
    public KeyCard findNewAddCard(int fromUser);

    @Update("update key_card set card_id=#{cardId} where from_user=#{fromUser} and card_id is NULL order by key_id desc limit 1")
    public int updateNewAddCard(@Param("cardId") long cardId,@Param("fromUser") int fromUser);

    @Delete("delete from key_card where from_user=#{fromUser} and card_id is NULL order by key_id desc limit 1")
    public int deleteNewAddCard(@Param("fromUser") int fromUser);



}
